
=begin
Copyright (C) 2014 Enrique Rando

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

http://www.gnu.org/licenses/gpl-2.0.html
=end

=begin

THIS CLASS IS MEANT FOR EDUCATIONAL PURPOSES ONLY

IF YOU NEED TO USE THIS SEARCH ENGINE, PLEASE IMPLEMENT IT IN A RUBY CLASS AS INDICATED IN THE DOCUMENTATION USING THE APPROPIATE APIs

=end

require 'uri'
require File.expand_path(File.dirname(__FILE__) + '/../Search_engine.rb')
require File.expand_path(File.dirname(__FILE__) + '/Google_deferred.rb')

class Google_web < Search_engine
	# Object creation
	# Parameters
	#		max_requests	maximum number of requests
	#		proxy			Proxy name or address. Use nil for no proxy.
	#						By default (:ENV), Ruby 2.1 uses HTTP_PROXY environment variable to set the proxy
	#		proxy_port		Proxy port
	#		proxy_user		Proxy username, if required
	#		proxy_password	Proxy password, if required.
	def initialize(max_requests=3, proxy=:ENV, proxy_port=nil, proxy_user=nil, proxy_password=nil)
		# Regexps for data recognition
		regexps = {
			:begin_result =>/<li class="g"/,
			:end_results => /<hr class="rgsep">\s*<\/ol>/,
			:document_type => /<span class="_ogd b w xsm">\[(?<data>.*?)\]<\/span>/,
			:title => /<a href.*?>(?<data>.*?)<\/a>/,
			:url => /<a href="(?<data>[^"]*?)"/,
			:summary => /<span class="st">(?<data>.*?)<\/span>/,
			:cache => /<a class="fl" href="(?<data>.*?)"/
		}

		# Mechanisms used for result paging
		paging = {
			:parameter => "start", 
			:parameter_method => :get, 
			:size_parameter => 'PREF',
			:size_parameter_method => :cookie,
			:size_parameter_value => 'ID=0111111111111111:NR=100', 
			:size => 100
		}

		# Create the Search Engine
		super(
			:https, "www.google.com", :default, "/search", "q",
			regexps, paging,
			{"filter"=>"0"}, "", {},
			"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0",
			"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",	
			max_requests
		)
		
		# Object to use to get deferred info
		set_deferred(Google_deferred.new("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0",
						proxy, proxy_port, proxy_user, proxy_password)
		)
		
		# Establish connection
		connect_through(proxy, proxy_port, proxy_user, proxy_password)
	end

private
	# Get rid of &amp; in URLs
	def get_url(result)
		return super(result).gsub('&amp;', '&')
	end
	
	# Force use of deferred data object for translation URLs
	def get_translate(result)
		return nil
	end
end


